package gui.facture;

import gui.FenetrePrincipale;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JButton;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import model.CatalogueChambre;
import model.Chambre;

import Controleur.ControleurFacture;

public class FenetreFactureSelectChambre extends FenetrePrincipale {
	private JList lstChambres;
	JButton btnTerminer;
	
	public FenetreFactureSelectChambre(ActionListener ctrl){
		afficher(ctrl);
	}
	
	@Override
	public void afficher(final ActionListener ctrl) {
		setBounds(100, 100, 523, 312);
		getContentPane().setLayout(null);
		
		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBounds(12, 42, 481, 174);
		getContentPane().add(scrollPane);
		
		lstChambres = new JList();
		ArrayList<String> tempsChambre = new ArrayList<String>();
		for (Chambre itChambre:CatalogueChambre.getInstance()){
			tempsChambre.add(itChambre.toString());
		}
		lstChambres.setListData(tempsChambre.toArray());
		lstChambres.addListSelectionListener(new ListSelectionListener() {
			
			@Override
			public void valueChanged(ListSelectionEvent arg0) {
				if (lstChambres.getSelectedIndex() >= 0) {
					ctrl.actionPerformed(new ActionEvent(this, lstChambres.getSelectedIndex(), 
							String.valueOf(ControleurFacture.AJOUTER_SEJOUR_FACTURE)));
				}
			}
		});
		scrollPane.setViewportView(lstChambres);
		
		JLabel lblSelectionnerLesChambres = new JLabel("Selectionner les chambres factur\u00E9");
		lblSelectionnerLesChambres.setBounds(12, 13, 301, 16);
		getContentPane().add(lblSelectionnerLesChambres);
		
		JPanel panelBtn = new JPanel();
		panelBtn.setBounds(12, 229, 481, 40);
		getContentPane().add(panelBtn);
		
		btnTerminer = new JButton("Terminer");
		btnTerminer.setEnabled(false);
		btnTerminer.addActionListener (ctrl);
		btnTerminer.setActionCommand(String.valueOf(ControleurFacture.AFFICHER_SOUS_TOTAL));
		panelBtn.add(btnTerminer);
		
		JButton btnCancel = new JButton("Annuler");
		btnCancel.addActionListener (ctrl);
		btnCancel.setActionCommand(String.valueOf(ControleurFacture.ANNULER));
		panelBtn.add(btnCancel);
		
		init();
		
		setVisible(true);
	}
	
	public void setTerminer(){
		btnTerminer.setEnabled(true);
	}
}
